package com.ljkj.cordial.chat.widget;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.sp.SPKeys;
import com.lanjiu.lib.utils.sp.SharedPreferenceHelper;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.adapter.ChatPopAdapter;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.MsgType;
import com.ljkj.cordial.chat.dao.ChatTableBean;
import com.nic.view.util.JToast;
import com.nic.view.util.Utility;
import com.nic.view.widget.CommonPopupWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 聊天条目操作按钮
 */
public class ChatItemPop extends CommonPopupWindow {
    public static final String DEL = "删    除";
    public static final String REDEL = "撤    回";
    public static final String MUL_CHOOSE = "多    选";
    public static final String NO_AUDIO_PLAY = "静音播放";

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.mac_switch)
    TextView macSwitch;

    /**
     * 消息下标
     */
    private int position;
    private ChatPopAdapter chatPopAdapter;
    private OnPlayModeChangeListener mOnPlayModeChangeListener;

    public ChatItemPop(Context c) {
        super(c, R.layout.dialog_chat_item_menu, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ButterKnife.bind(this, contentView);
        getPopupWindow().setOutsideTouchable(true);
        chatPopAdapter = new ChatPopAdapter();
        listView.setAdapter(chatPopAdapter);
//        setDataAndTransmit();
        macSwitch.setOnClickListener(v -> {
            getPopupWindow().dismiss();
            String key = SPKeys.Key.CONFIG_VOICE_PLAY_BY_MAC + UserPreferences.getUserId();
            boolean isMac = SharedPreferenceHelper.create(SPKeys.Name.CONFIG).getBoolean(key, Canstants.DEFAULT_MAC);
            if (mOnPlayModeChangeListener != null)
                mOnPlayModeChangeListener.onPlayModeChange(!isMac);
            if (isMac) {
                SharedPreferenceHelper.create(SPKeys.Name.CONFIG).setBoolean(key, false);
                macSwitch.setText("扬声器模式");
                JToast.showShort(macSwitch.getContext(), "已切换为扬声器播放模式");
            } else {
                SharedPreferenceHelper.create(SPKeys.Name.CONFIG).setBoolean(key, true);
                macSwitch.setText("听筒模式");
                JToast.showShort(macSwitch.getContext(), "已切换为听筒播放模式");
            }
        });
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                getPopupWindow().dismiss();
                if (listener != null) {
                    listener.onItemClick(parent, view, position, id);
                }
            }
        });
    }

    public void setData(List<String> list) {
        chatPopAdapter.setList(list);
        Utility.setListViewHeightBasedOnChildren(listView);
    }

    public void setData(String text) {
        List<String> list = new ArrayList<>();
        list.add(text);
        chatPopAdapter.setList(list);
        Utility.setListViewHeightBasedOnChildren(listView);
    }

    public void setData(int sendType) {
        List<String> list = new ArrayList<>();
        if (sendType == ChatTableBean.SEND) {
            list.add(REDEL);
        } else {
            list.add(DEL);
        }
        list.add(MUL_CHOOSE);
        chatPopAdapter.setList(list);
        Utility.setListViewHeightBasedOnChildren(listView);
    }

    public void setDataAndTransmit(int sendType, int chatType, int uiType) {
        List<String> list = new ArrayList<>();
        if (sendType == ChatTableBean.SEND) {
            list.add(REDEL);
        } else {
            list.add(DEL);
        }
        list.add(MUL_CHOOSE);
//        list.add("转发好友");
//        list.add("转发群组");
        macSwitch.setVisibility(View.GONE);
        if (chatType == MsgType.Voice) {
            macSwitch.setVisibility(View.VISIBLE);
            String key = SPKeys.Key.CONFIG_VOICE_PLAY_BY_MAC + UserPreferences.getUserId();
            boolean isMac = SharedPreferenceHelper.create(SPKeys.Name.CONFIG).getBoolean(key, Canstants.DEFAULT_MAC);
            if (isMac)
                macSwitch.setText("扬声器模式");
            else
                macSwitch.setText("听筒模式");
        } else {
            if (chatType == MsgType.Video) {
                list.add(NO_AUDIO_PLAY);
            }
        }
        chatPopAdapter.setList(list);
        Utility.setListViewHeightBasedOnChildren(listView);
    }

    public String getPositionItem(int position) {
        return (String) chatPopAdapter.getItem(position);
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public int getPosition() {
        return position;
    }

    public void setOnPlayModeChangeListener(OnPlayModeChangeListener listener) {
        this.mOnPlayModeChangeListener = listener;
    }

    public interface OnPlayModeChangeListener {
        void onPlayModeChange(boolean isMac);
    }
}
